package main

import (
	"crypto/sha512"
	"encoding/hex"
	"encoding/json"
	"fmt"

	"strings"
)

/*
   function _getHashKey($posted, $salt_key){
       $hash_sequence = "key|txnid|amount|productinfo|firstname|email|udf1|udf2|udf3|udf4|udf5|udf6|udf7|udf8|udf9|udf10";

       // make an array or split into array base on pipe sign.
       $hash_sequence_array = explode('|', $hash_sequence);
       $hash = null;

       // prepare a string based on hash sequence from the $params array.
       foreach ($hash_sequence_array as $value) {
           $hash .= isset($posted[$value]) ? $posted[$value] : '';
           $hash .= '|';
       }

       $hash .= $salt_key;
       #echo $hash;
       #echo " ";
       #echo strtolower(hash('sha512', $hash));
       // generate hash key using hash function(predefine) and return
       return strtolower(hash('sha512', $hash));
   }
 */

func GetHashKey(mp map[string]string) string{
	hash_sequence := "key|txnid|amount|productinfo|firstname|email|udf1|udf2|udf3|udf4|udf5|udf6|udf7|udf8|udf9|udf10"
	hash_sequence_array := strings.Split(hash_sequence,"|")
	hash := ""
	for _, v := range hash_sequence_array {
		hash = hash + mp[v] + "|"
	}
	saltKey := "VQSF7VA8V5"
	hash = hash + saltKey
	fmt.Println("111", hash)
	hashObj := sha512.New()
	hashObj.Write([]byte(hash))
	hashString := hex.EncodeToString(hashObj.Sum(nil))
	fmt.Println("222", hashString)
	return hashString
}

func main()  {
	fmt.Println("________test__")
	/*
	"txnid" => "T3SAT0B5OL",
	"amount" => "100.0",
	"firstname" => "jitendra",
	"email" => "test@gmail.com",
	"phone" => "1231231235",
	"productinfo" => "Laptop",
	"surl" => "http://localhost:3000/response.php",
	"furl" => "http://localhost:3000/response.php",
	 */
	mp := map[string]string{
		"key" : "I3ZR1Y6LOC",
		"txnid" : "T3SAT0B5OL1",
		"amount" : "110",
		"firstname" : "jitendra",
		"email" : "test@gmail.com",
		"phone" : "1231231235",
		"productinfo" : "Laptop",
		"surl" : "http://localhost:3000/response.php",
		"furl" : "http://localhost:3000/response.php",
		"udf1" : "aaaa",
		"udf2" : "aaaa",
		"udf3" : "aaaa",
		"udf4" : "aaaa",
		"udf5" : "aaaa",
		"address1" : "aaaa",
		"address2" : "aaaa",
		"city" : "aaaa",
		"state" : "aaaa",
		"country" : "aaaa",
		"zipcode" : "123123",
	}
	str := GetHashKey(mp)
	fmt.Println("str ", str)

	strJson := `{
    "entity": "collection",
    "count": 1,
    "items": [
        {
            "id": "pay_FfdXqhlUat6mbp",
            "entity": "payment",
            "amount": 4900900,
            "currency": "INR",
            "status": "captured",
            "order_id": "order_FfdWrJBh8PEeMz",
            "invoice_id": null,
            "international": false,
            "method": "card",
            "amount_refunded": 0,
            "refund_status": null,
            "captured": true,
            "description": "A Wild Sheep Chase",
            "card_id": "card_FfbN4sBDzQAqmo",
            "bank": null,
            "wallet": null,
            "vpa": null,
            "email": "ritaffaella@outlook.com",
            "contact": "+919821169383",
            "notes": {
                "shipping address": "L-16, The Business Centre, 61 Wellfield Road, New Delhi - 110001"
            },
            "fee": 115662,
            "tax": 17644
		}]
	}`

	//mp2 := map[string]interface{}{}
	//json.Unmarshal([]byte(strJson), &mp2)
	//
	//mp3 := razorpayGetStringMap(mp2["items"].([]map[string]interface{})[0])
	//
	//fmt.Printf("mp3:%v \n", mp3)
	//fmt.Println(reflect.TypeOf(mp3["err"]), mp3["err"], len(mp3["xx"]))


	res := RazorpayQueryRes{}
	json.Unmarshal([]byte(strJson), &res)

	fmt.Printf("res:%+v \n", res)

	fmt.Println(len("MAL_D_6_F26CD341B59B84DA683D5A653BE99959"))

	type Header map[string][]string
    type R struct{
		Header Header
	}
	r := R{}
	head := r.Header
	if head["xxx"] == nil {
		fmt.Println("11", len(head["xxx"]))
	}
	fmt.Println(head["xxx"][0])


}

func razorpayGetStringMap(mp map[string]interface{}) map[string]string {
	ret := make(map[string]string, len(mp))
	for k, v := range mp {
		ret[k] = fmt.Sprint(v)
	}
	return ret
}



type RazorpayQueryRes struct {
	Entity string                 `json:"entity"`
	Count  int                    `json:"count"`
	Items  []RazorpayQueryResItem `json:"items"`
	Error  RazorpayQueryResError  `json:"error"`

}

type RazorpayQueryResItem struct {
	Id              string `json:"id"`
	Entity          string `json:"entity"`
	Amount          float64 `json:"amount"`
	Currency        string `json:"currency"`
	Status          string `json:"status"`
	Order_id        string `json:"order_id"`
	Invoice_id      string `json:"invoice_id"`
	International   bool `json:"international"`
	Method          string `json:"method"`
	Amount_refunded string `json:"amount_refunded"`
	Refund_status   string `json:"refund_status"`
	Captured        bool `json:"captured"`
	Description     string `json:"description"`
	Card_id         string `json:"card_id"`
	Bank            string `json:"bank"`
	Wallet          string `json:"wallet"`
	Vpa             string `json:"vpa"`
	Email           string `json:"email"`
	Contact         string `json:"contact"`
	Fee             float64 `json:"fee"`
	Tax             float64 `json:"tax"`

}

type RazorpayQueryResError struct {
	Code        string `json:"code"`
	Description string `json:"description"`
	Source      string `json:"source"`
	Step        string `json:"step"`
	Reason      string `json:"reason"`
	Metadata    string `json:"metadata"`
	Field       string `json:"field"`
}